package server.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.demo.dto.ApiResponse;
import server.demo.dto.CreateSeatRequest;
import server.demo.dto.SeatDTO;
import server.demo.service.SeatService;

import jakarta.validation.Valid;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/v1/seats")
@CrossOrigin(origins = "*")
public class SeatController {
    
    @Autowired
    private SeatService seatService;
    
    @GetMapping("/carriage/{carriageId}")
    public ApiResponse<List<SeatDTO>> getSeatsByCarriageId(@PathVariable Long carriageId) {
        try {
            List<SeatDTO> seats = seatService.getSeatsByCarriageId(carriageId);
            return ApiResponse.success(seats);
        } catch (Exception e) {
            return ApiResponse.error("获取座位列表失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/train/{trainId}")
    public ApiResponse<List<SeatDTO>> getSeatsByTrainId(@PathVariable Long trainId) {
        try {
            List<SeatDTO> seats = seatService.getSeatsByTrainId(trainId);
            return ApiResponse.success(seats);
        } catch (Exception e) {
            return ApiResponse.error("获取座位列表失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/train/{trainId}/available")
    public ApiResponse<List<SeatDTO>> getAvailableSeatsByTrainId(@PathVariable Long trainId) {
        try {
            List<SeatDTO> seats = seatService.getAvailableSeatsByTrainId(trainId);
            return ApiResponse.success(seats);
        } catch (Exception e) {
            return ApiResponse.error("获取可用座位失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/carriage/{carriageId}/available")
    public ApiResponse<List<SeatDTO>> getAvailableSeatsByCarriageId(@PathVariable Long carriageId) {
        try {
            List<SeatDTO> seats = seatService.getAvailableSeatsByCarriageId(carriageId);
            return ApiResponse.success(seats);
        } catch (Exception e) {
            return ApiResponse.error("获取可用座位失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/{id}")
    public ApiResponse<SeatDTO> getSeatById(@PathVariable Long id) {
        try {
            Optional<SeatDTO> seat = seatService.getSeatById(id);
            if (seat.isPresent()) {
                return ApiResponse.success(seat.get());
            } else {
                return ApiResponse.error("座位不存在");
            }
        } catch (Exception e) {
            return ApiResponse.error("获取座位信息失败: " + e.getMessage());
        }
    }
    
    @PostMapping
    public ApiResponse<SeatDTO> createSeat(@Valid @RequestBody CreateSeatRequest request) {
        try {
            SeatDTO seat = seatService.createSeat(request);
            return ApiResponse.success("座位创建成功", seat);
        } catch (Exception e) {
            return ApiResponse.error("创建座位失败: " + e.getMessage());
        }
    }
    
    @PutMapping("/{id}")
    public ApiResponse<SeatDTO> updateSeat(@PathVariable Long id, @Valid @RequestBody CreateSeatRequest request) {
        try {
            SeatDTO seat = seatService.updateSeat(id, request);
            return ApiResponse.success("座位更新成功", seat);
        } catch (Exception e) {
            return ApiResponse.error("更新座位失败: " + e.getMessage());
        }
    }
    
    @PutMapping("/{id}/toggle")
    public ApiResponse<Void> toggleSeatAvailability(@PathVariable Long id) {
        try {
            seatService.toggleSeatAvailability(id);
            return ApiResponse.success("座位状态切换成功", null);
        } catch (Exception e) {
            return ApiResponse.error("切换座位状态失败: " + e.getMessage());
        }
    }
    
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteSeat(@PathVariable Long id) {
        try {
            seatService.deleteSeat(id);
            return ApiResponse.success("座位删除成功", null);
        } catch (Exception e) {
            return ApiResponse.error("删除座位失败: " + e.getMessage());
        }
    }
}